package com.sgb.kjwl.viewmodel.self.offer;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.TruckApplyEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Describe:    货车报名ViewModel
 * Author:  yangLei
 * Date:    2021/4/7
 */
public class TruckApplyModel extends AndroidViewModel {
    private int pageSize = 10;
    private int currPage = 1;
    public MutableLiveData<List<TruckApplyEntity>> refreshLiveData = new MutableLiveData<>();
    public MutableLiveData<List<TruckApplyEntity>> loadMoreLiveData = new MutableLiveData<>();

    public TruckApplyModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 获取报价货车列表信息
     *
     * @param refresh 是否刷新
     */
    public void getTrucks(Context activity, boolean refresh,boolean isGeneralGoods) {
        currPage = refresh ? 1 : currPage + 1;
        Map map = new HashMap();
        map.put("page", currPage);
        map.put("limit", pageSize);
        //根据当前的身份进行传参:1公司 2团队 3个人
        if (UserOperating.getInstance().isCompany()) {
            map.put("companyNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
        } else if (UserOperating.getInstance().isTeam()) {
            map.put("teamNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
        } else if (UserOperating.getInstance().isPersonal()) {
            map.put("terminal", 3);
        }
        map.put("pattern",isGeneralGoods?1:0);
        DialogHelper.showProgressDialog(activity, "数据加载中...");
        NewAPIManager.getInstance().getLogisticsAPI().getApplyTruckList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TruckApplyEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TruckApplyEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() != null)
                        if (refresh) {
                            refreshLiveData.postValue(data.getData().getList());
                        } else {
                            loadMoreLiveData.postValue(data.getData().getList());
                        }
                }
            }
        });
    }
}
